<template>
	<view style="padding-bottom: 100rpx;">
		<van-skeleton title row="13" :loading="loading">
		<view class="order-content">
			
			<view style="margin-bottom: 40rpx;">

				<view class="detail-title">
					<text>考核信息</text>
				</view>
				<view class="content-item">
					<text class="item-name">考核项目：</text>
					<view class="right-content">{{ assessInfo['考核项目']}}</view>
				</view>
				<view class="content-item">
					<text class="item-name">考核范围：</text>
					<view class="right-content">{{ assessInfo['考核范围名称']}}</view>
				</view>
				<view v-if="projName" class="content-item">
					<text class="item-name">考核小区：</text>
					<view class="right-content">{{ projName }}</view>
				</view>
				<view class="content-item">
					<text class="item-name">考核类型：</text>
					<view class="right-content">{{ assessInfo['考核类型名称']}}</view>
				</view>
				<view class="content-item">
					<text class="item-name">考核频率：</text>
					<view class="right-content">{{ type[assessInfo['考核频率']] + assessInfo['考核次数'] + '次'}}</view>
				</view>
				<view class="content-item">
					<text class="item-name">已考核次数：</text>
					<view class="right-content">{{ assessInfo['已考核次数'] + '次'}}</view>
				</view>
			</view>
			<view style="margin-bottom: 40rpx;">
				<view class="detail-title">
					<text>考核事项</text>
				</view>
			</view>
		</view>
		<!-- <view style="margin-top: -140rpx;">
			<assess-table @submit="onSubmit" ref="assessTable" :list="assessInfo['考核事项']"></assess-table>
		</view> -->
		<view v-for="item in assessInfo['考核事项']" :key="item.id">
			<view  class="check-title">{{item['事项名称']}}</view>
			<view v-if="item['考核细则']">
				<view  class="check-item" :key="subItem.id" v-for="(subItem,index) in item['考核细则']">
					<view>
						{{index+1}}、{{subItem['考核要求']}}（上限{{subItem['分数上限']}}分）
					</view>
					<view v-if="item['事项结果类型'] == 2" style="display: flex;align-items: center;justify-content: space-between;">
						<text>分类正确率(%):</text>
						<van-stepper :decimal-length="2" @change="percentChange(item, subItem, $event)" input-width="120rpx"  button-size="64rpx"  :value="subItem['百分比值']" min="0" max="100" />
					</view>
					<view class="check-result">
						<view @click="switchShow(subItem)" style="color: #1152FC;">{{subItem.show ? '隐藏':'上传图片'}}</view>
						
						<view style="display: flex;align-items: center;">
							<text style="margin-right: 20rpx;">扣分:</text>
							<van-stepper @change="onChange(item, subItem, $event)" input-width="120rpx"  button-size="64rpx"  :value="subItem['得分']" min="0" :max="subItem['分数上限']" />
						</view>
					</view>
					<unquality-assess @input="handleInput(item, subItem, $event)" @change="handleChange(item, subItem, $event)" @upload="handleUpload(item, subItem, $event)" v-show="subItem.show"></unquality-assess>
				</view>
			</view>
			
			<view v-if="item['子事项']">
				<view  class="check-item" v-for="(subItem,index) in item['子事项']" :key="subItem.id">
					<view  class="check-child-title">{{subItem['事项名称']}}</view>
					
					<view v-for="childItem in subItem['考核细则']" :key="childItem.id">
						<view>
							{{index+1}}、{{childItem['考核要求']}}（上限{{childItem['分数上限']}}分）
						</view>
						<view v-if="subItem['事项结果类型'] == 2" style="display: flex;align-items: center;justify-content: space-between;">
							<text>分类正确率(%):</text>
							<van-stepper :decimal-length="2" @change="percentChange(subItem, childItem, $event)" input-width="120rpx"  button-size="64rpx"  :value="childItem['百分比值']" :min="0" :max="100" />
						</view>
						<view class="check-result">
							<view @click="switchShow(childItem)" style="color: #1152FC;">{{childItem.show ? '隐藏':'上传图片'}}</view>
							<view style="display: flex;align-items: center;">
								<text style="margin-right: 20rpx;">扣分:</text>
								<van-stepper @change="onChange(subItem, childItem, $event)" input-width="120rpx"  button-size="64rpx"  :value="childItem['得分']" min="0" :max="childItem['分数上限']" />
							</view>
						</view>
						<unquality-assess @input="handleInput(subItem, childItem, $event)" @change="handleChange(subItem, childItem, $event)" @upload="handleUpload(subItem, childItem, $event)" v-show="childItem.show"></unquality-assess>
					</view>
				</view>
			</view>
			
		</view>
		
		<button @click="onSubmit" class="assess-submit-btn">提交</button>
		</van-skeleton>
	</view>

</template>

<script>
	import VanSwitch from '@/wxcomponents/vant/dist/switch'
	import upload from '@/components/upload.vue'
	import UnqualityAssess from '../components/unqualityAssess.vue'
	export default {
		components: {
			VanSwitch,
			upload,
			UnqualityAssess
		},
		data() {
			return {
				loading: true,
				projName: '',
				type: {
					1: '每年',
					2: '每月',
					3: '每周',
					4: '每日'
				},
				isShow: false,
				assessInfo: {},
				params: {
					项目id: null,
					考核id: null,
					考核事项: []
				}
			}
		},
		onLoad(options) {
			if(options.projName) {
				this.projName = options.projName
			}
			this.params.项目id = +options.projId
			this.params.考核id = +options.id
			this.getAssessInfo(+options.id, +options.projId)
			// this.getAssessCondition(+options.id)
		},
		methods: {
			switchShow(item) {
				item.show = !item.show
			},
			percentChange(obj, childObj, e) {
				const item = this.params.考核事项.find(item => item.事项id === obj.id && item.内容id === childObj.id)
				item.百分比值 = e.detail
				childObj.百分比值 = e.detail
			},
			onChange(obj, childObj, e) {
				const item = this.params.考核事项.find(item => item.事项id === obj.id && item.内容id === childObj.id)
				item.得分 = e.detail
				childObj.得分 = e.detail
			},
			handleInput(obj, childObj, e) {
				const item = this.params.考核事项.find(item => item.事项id === obj.id && item.内容id === childObj.id)
				item.问题描述 = e
			},
			handleChange(obj, childObj, e) {
				const item = this.params.考核事项.find(item => item.事项id === obj.id && item.内容id === childObj.id)
				item.checkd = e
				if(e) {
					item.得分 = 0
					childObj.得分 = 0
				}else {
					item.得分 = (item.附件.length * (childObj.考核标准扣分 / childObj.考核标准项数)).toFixed(2)
					childObj.得分 = (item.附件.length * (childObj.考核标准扣分 / childObj.考核标准项数)).toFixed(2)
				}
				
			},
			handleUpload(obj, childObj, e) {
				const item = this.params.考核事项.find(item => item.事项id === obj.id && item.内容id === childObj.id)
				item.附件 = e
				if(!childObj.checked) {
					childObj.得分 = (item.附件.length * (childObj.考核标准扣分 / childObj.考核标准项数)).toFixed(2)
					if(childObj.得分 > childObj.分数上限) {
						childObj.得分 = childObj.分数上限
					}
					item.得分 = childObj.得分
				}
			},
			onSubmit() {
				this.handleSubmit()
			},
			handleSubmit() {
				uni.showLoading()
				this.params.考核事项.forEach(item => {
					if(typeof item['得分'] === 'string') {
						item.得分 = +item.得分
					}
					if(typeof item['百分比值'] === 'string') {
						item.百分比值 = parseFloat(item.百分比值)
					}
				})
				this.$http2.post('/transactionCompete/evaluate', this.params).then(res => {
					if(res.code === 10000) {
						uni.redirectTo({
							url: '/ticketSystem/acceptSuccess/acceptSuccess?path=/pages/service/service' + '&title=返回服务页面'
						})
						// uni.redirectTo({
						// 	url: '/ticketSystem/acceptSuccess/acceptSuccess?path=/assessSystem/index/index' + '&title=返回考评首页'
						// })
					}
				})
			},
			async getAssessInfo(id, projId) {
				uni.showLoading()
				const res = await this.$http2.post('transactionCompete/assessmentManagement/info', {id, 考核小区id: projId})
				if (res.code === 10000) {
					res.data.考核事项.forEach(item => {
						item.考核细则 = item.考核细则 || []
						item.子事项 = item.子事项 || []
					})
					for(let i = 0; i < res.data.考核事项.length; i++) {
						for(let j = 0; j < res.data.考核事项[i].考核细则.length; j++) {
							res.data.考核事项[i].考核细则[j].show = false
							this.params.考核事项.push({
								事项id: res.data.考核事项[i].id,
								内容id: res.data.考核事项[i].考核细则[j].id,
								checked: false,
								得分: null,
								附件: [],
								百分比值: null,
								问题描述: null
							})
						}	
					 }
					 let childCheck = res.data.考核事项.map(item => item.子事项).reduce((a,b) => { return a.concat(b)})
					 for(let i = 0; i < childCheck.length; i++) {
					 	for(let j = 0; j < childCheck[i].考核细则.length; j++) {
					 		childCheck[i].考核细则[j].show = false
					 		this.params.考核事项.push({
					 			事项id: childCheck[i].id,
					 			内容id: childCheck[i].考核细则[j].id,
					 			checked: false,
					 			得分: null,
					 			附件: [],
								百分比值: null,
					 			问题描述: null
					 		})
					 	}	
					  }
					// console.log('child', childCheck)
					this.assessInfo = res.data
					this.loading = false
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import '../../style/info.scss';
	.unquality-assess-item {
		width: 700rpx;
		height: 360rpx;
		padding: 20rpx;
		border: 2rpx solid #B5B5B5;
		margin-bottom: 20rpx;
		.top-box {
			display: flex;
			justify-content: space-between;
		}
		.desc {
			margin-bottom: 20rpx;
			display: flex;
			input {
				width: 450rpx;
			}
		}
		.img-content {
			display: flex;
			margin-left: -20rpx;
			.img-item {
				width: 140rpx;
				height: 140rpx;
				margin: 10rpx;
				.img {
					width: 100%;
					height: 100%;
				}
			}
		}
	}
	.order-content {
		margin-bottom: 0;
	}
	.check-title {
			height: 80rpx;
			font-size: 40rpx;
			text-align: center;
			line-height: 60rpx;
			border-bottom: 2rpx solid #DEDEDE;
			margin-bottom: 30rpx;
		}
	.check-child-title {
			height: 60rpx;
			font-size: 32rpx;
			text-align: center;
			line-height: 40rpx;
			border-bottom: 2rpx solid #DEDEDE;
			margin-bottom: 30rpx;
		}
	
		.check-item {
			margin-bottom: 40rpx;
			font-size: 32rpx;
			padding: 0 20rpx;
			.unquality-check-item {
				width: 674rpx;
				height: 258rpx;
				padding: 20rpx;
				border: 2rpx solid #B5B5B5;
				margin-bottom: 20rpx;
	
				.desc {
					margin-bottom: 20rpx;
					display: flex;
	
					input {
						width: 450rpx;
					}
				}
	
				.img-content {
					display: flex;
	
					.img-item {
						width: 140rpx;
						height: 140rpx;
						margin: 10rpx;
	
						.img {
							width: 100%;
							height: 100%;
						}
					}
				}
			}
		}
	
		.check-result {
			height: 100rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			.switch {
				color: #3080FB;
			}
		}
	
	.assess-title {
		text-align: center;
		font-size: 32rpx;
		font-weight: bold;
	}
	.assess-submit-btn {
		position: fixed;
		height: 98rpx;
		text-align: center;
		color: #fff;
		line-height: 98rpx;
		left: 0;
		right: 0;
		bottom: 0;
		border-radius: 0;
		background-color: #1152FC;
		z-index: 99;
		&::after {
			border-radius: 0;
		}
	}
</style>
